﻿///#pragma execution_character_set("utf-8")

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGridLayout>
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <lua.hpp>
#include <sol/sol.hpp>

int Max(int a,int b,int c)
{
    int temp = qMax(a,b);
    return qMax(temp,c);
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    ,m_pCourse(nullptr)
    ,m_pStudentScore(nullptr)
{
    ui->setupUi(this);

    initUI();

    connect(ui->actionrun,SIGNAL(triggered(bool)),this,SLOT(test_run()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initUI()
{
    QGridLayout* grid_layout = new QGridLayout();

    QWidget* center_widget = new QWidget();


    m_pLuaEditor = new LuaEditor();
    m_pCourse = new Course();
    m_pStudentScore = new StudentScore();
    p_plain = new QPlainTextEdit();
    p_plain->setMaximumHeight(200);

    grid_layout->addWidget(m_pLuaEditor,0,0,3,1);
    grid_layout->addWidget(p_plain,3,0,1,1);
    grid_layout->addWidget(m_pCourse,0,1,2,1);
    grid_layout->addWidget(m_pStudentScore,2,1,2,1);

    center_widget->setLayout(grid_layout);
    setCentralWidget(center_widget);
}

void MainWindow::test_run()
{
    sol::state lua;

    lua.open_libraries(sol::lib::base, sol::lib::coroutine, sol::lib::string, sol::lib::io);

    lua["Max"] = &Max;

    auto result = lua.script(m_pLuaEditor->text().toStdString());

    for(auto iter = result.begin();iter != result.end();iter++)
    {
        p_plain->appendPlainText(QString("result:")+QString::number(iter->get<int>()));
    }
}

